package com.gree.scmservice.controller;

import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.enums.BusinessType;
import com.gree.framework.entity.SysUserEx;
import com.gree.scmservice.service.GscmSysUserExService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 虚拟用户(password模式获取token，与gscmmode模式不冲突获取token)
 * Created by 180686 on 2021/3/17 14:48
 */

@RestController
@RequestMapping("/sysuserex")
public class SysUserExController {

    @Autowired
    private GscmSysUserExService gscmSysUserExService;

    /**
     * 注册虚拟账号
     * @return
     */
    @Log(title = "虚拟账号日志", businessType = BusinessType.INSERT)
    @PostMapping
    public Object add(@RequestBody SysUserEx sysUserEx){
        gscmSysUserExService.insertSysUserEx(sysUserEx);
        return new ReturnVo("200","success");
    }

    /**
     * 显示虚拟账号
     * @return
     */
    @GetMapping("/list")
    public Object list(SysUserEx sysUserEx){
        List<SysUserEx> list = gscmSysUserExService.selectSysUserExList(sysUserEx);
        return new ReturnVo("200","success", list);
    }

    /**
     * 修改虚拟账号
     */
    @Log(title = "虚拟账号日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public ReturnVo edit(@RequestBody SysUserEx sysUserEx)
    {
        sysUserEx.setUpdateDt(LocalDateTime.now());
        int resultNum = gscmSysUserExService.updateSysUserEx(sysUserEx);
        if(resultNum > 0){
            return new ReturnVo("200","success");
        }else{
            return new ReturnVo("500","fail");
        }
    }

    /**
     * 删除虚拟账号
     */
    @Log(title = "虚拟账号日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{username}")
    public ReturnVo remove(@PathVariable("username") String username)
    {
        int resultNum = gscmSysUserExService.deleteSysUserExByName(username);
        if(resultNum > 0){
            return new ReturnVo("200","success");
        }else{
            return new ReturnVo("500","fail");
        }
    }

    /**
     * 注册虚拟账号
     * @return
     */
    @Log(title = "虚拟账号日志", businessType = BusinessType.INSERT)
    @GetMapping("/reset/{username}")
    public Object reset(@PathVariable("username") String username){
        int resultNum = gscmSysUserExService.resetPassword(username);
        if(resultNum > 0){
            return new ReturnVo("200","success");
        }else{
            return new ReturnVo("500","fail");
        }
    }

}
